let express = require("express");
let path = require("path");
let moment = require("moment");//引入日期格式类库
let accountDao = require("../server/account");
let indexDao = require("../server/index")
let userDao = require("../server/user")
let walletDao = require("../server/wallet")
let productDao = require("../server/product")
let trolleyDao = require("../server/trolley");
let orderDao = require("../server/order");
let payDao = require("../server/pay");
const evaluateDao = require("../server/evaluate");

let router = express.Router(); // 创建路由实例

/* ********** 发送页面 ********** */
// 主页
router.get(["/","/index.html"],function(req,res){
    res.sendFile(path.join(__dirname,"../view/index.html"))
})
// 登陆注册页面
router.get("/account",function(req,res){
    res.sendFile(path.join(__dirname, "../view/account.html"))
})
// 找回密码页面
router.get("/forget",function(req,res){
    res.sendFile(path.join(__dirname, "../view/forget.html"))
})
// 手机页面
router.get("/allphone",function(req,res){
    res.sendFile(path.join(__dirname, "../view/allphone.html"))
})
// 购物车
router.get("/trolley",function(req,res){
    res.sendFile(path.join(__dirname, "../view/trolley.html"))
})
// 电脑页面
router.get("/allcomputer",function(req,res){
    res.sendFile(path.join(__dirname, "../view/allcomputer.html"))
})
// 隐私页面
router.get("/privacy",function(req,res){
    res.sendFile(path.join(__dirname, "../view/privacy.html"))
})
// 关于页面
router.get("/about",function(req,res){
    res.sendFile(path.join(__dirname, "../view/about.html"))
})
// 个人中心
router.get("/user",function(req,res){
    res.sendFile(path.join(__dirname, "../view/user.html"))
})
// 我的钱包
router.get("/wallet",function(req,res){
    res.sendFile(path.join(__dirname, "../view/wallet.html"))
})
// pay 页面
router.get("/pay",function(req,res){
    res.sendFile(path.join(__dirname, "../view/pay.html"))
})
// 订单页面
router.get("/order",function(req,res){
    res.sendFile(path.join(__dirname, "../view/order.html"))
})
// 我的评价页面
router.get("/uevaluate",function(req,res){
    res.sendFile(path.join(__dirname, "../view/uevaluate.html"))
})
/* ********** 发送数据 ********** */
// 用户注册
router.post("/api/register",function(req,res){
    //req.body接收前台发送的数据
    let username = req.body.username;
    let password = req.body.password;
    let tel = req.body.tel;
    accountDao.addUser(username,password,tel,function(ret){
        if(ret===1) {
            res.json({
                msg: "注册成功",
                code: 1001
    
            })
        } else if(ret===2) {
            res.json({
                msg: "注册失败,该手机号已被注册",
                code: 1002
                })
        } else if(ret===3) {
            res.json({
                msg: "注册失败,用户名已存在",
                code: 1003
                })
        } else {
            res.json({
                msg: "注册失败",
                code: 1003
            })
        }
    })
})
// 用户登陆
router.post("/api/login",function(req,res) {
    let username = req.body.username
    let password = req.body.password
    accountDao.checkInfo(username, password, function(ret) {
        res.json({
            code: ret.code,
            id: ret.id,
            token: ret.token
        })
    })
})
// 找回密码
router.post("/api/forget",function(req,res) {
    let username = req.body.username
    let tel = req.body.tel
    accountDao.getPwd(username, tel, function(ret) {
        res.json({
            code: ret.code,
            pwd: ret.pwd
        })
    })
})
// 首页验证
router.post("/api/index",function(req,res) {
    let id = req.body.id
    let token = req.body.token
    indexDao.checkInfo(id, token, function(ret) {
        res.json({
            code: ret.code,
            user: ret.user
        })
    })
})
// 手机页数据
router.post("/api/allphone",function(req,res) {
    productDao.allphone(function(ret) {
        res.json({
            code: ret.code,
            data: ret.data
        })
    })
})
// 电脑页数据
router.post("/api/allcomputer",function(req,res) {
    productDao.allcomputer(function(ret) {
        res.json({
            code: ret.code,
            data: ret.data
        })
    })
})
// 个人数据
router.post("/api/user",function(req,res) {
    let id = req.body.id
    let token = req.body.token
    userDao.getInfo(id, token, function(ret) {
        res.json({
            code: ret.code,
            uid: ret.uid,
            user: ret.user,
            tel: ret.tel,
            address: ret.address,
            unpaid_order: ret.unpaid_order,
            pending_order: ret.pending_order,
            review_order: ret.review_order,
        })
    })
})
// 更新个人信息
router.post("/api/userupdate",function(req,res) {
    let id = req.body.id
    let token = req.body.token
    let username = req.body.username
    let tel = req.body.tel
    let address = req.body.address
    userDao.updateInfo(id, token, username, tel, address, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 我的钱包数据
router.post("/api/wallet",function(req,res) {
    let id = req.body.id
    let token = req.body.token
    walletDao.getInfo(id, token, function(ret) {
        res.json({
            code: ret.code,
            user: ret.user,
            money: ret.money,
            trade: ret.trade
        })
    })
})
// 我的钱包-充值
router.post("/api/walletadd",function(req,res) {
    let id = req.body.id
    let token = req.body.token
    let money = req.body.money
    walletDao.addbalance(id, token, money, function(ret) {
        res.json({
            code: ret.code,
            user: ret.user,
            money: ret.money,
            trade: ret.trade
        })
    })
})
// 商品详情页
router.get('/product', function(req, res) {
    const productName = req.query.name;
    productDao.getProductData(productName, function(productData) {
        res.render('product', { product: productData })
    })
})
// 评价
router.get('/onevaluate', function(req, res) {
    const productName = req.query.name;
    productDao.getEvaluate(productName, function(productData) {
        res.render('evaluate', { product: productData })
    })
})
// 添加购物车
router.post('/api/addtrolley', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let pid = req.body.pid
    trolleyDao.addTrolley(uid, token, pid, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 获取购物车数据
router.post('/api/trolley', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    trolleyDao.getInfo(uid, token, function(ret) {
        res.json({
            code: ret.code,
            data: ret.data
        })
    })
})
// 获取购物车总数
router.post('/api/trolleycount', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    trolleyDao.getCount(uid, token, function(ret) {
        res.json({
            code: ret.code,
            count: ret.count
        })
    })
})
// 删除购物车货物
router.delete('/api/trolley', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let tid = req.body.tid
    trolleyDao.deleteProduct(uid, token, tid, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 创建订单
router.post('/api/createorder', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let tid = req.body.tid
    orderDao.addInfo(uid, token, tid, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 支付
router.post('/api/pay', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    payDao.getInfo(uid, token, function(ret) {
        res.json({
            code: ret.code,
            data: ret.data
        })
    })
})
// 修改地址
router.post('/api/payupdate', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let address = req.body.address
    payDao.updateAddress(uid, token, address, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 支付金额
router.post('/api/paygo', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let oid = req.body.oid
    let money = req.body.money
    let pwd = req.body.pwd
    payDao.goPay(uid, token, oid, money, pwd, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 订单数据
router.post('/api/order', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    orderDao.getInfo(uid, token, function(ret) {
        res.json({
            code: ret.code,
            data: ret.data
        })
    })
})
// 确认收货
router.post('/api/orderconfirm', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let oid = req.body.oid
    orderDao.confirmOrder(uid, token, oid, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 评价
router.post('/api/orderevaluate', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let oid = req.body.oid
    let text = req.body.text
    orderDao.evaluateOrder(uid, token, oid, text, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 删除订单
router.delete('/api/order', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let oid = req.body.oid
    orderDao.deleteOrder(uid, token, oid, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})
// 我的评价
router.post('/api/evaluate', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    evaluateDao.getInfo(uid, token, function(ret) {
        res.json({
            code: ret.code,
            data: ret.data
        })
    })
})
// 删除评价
router.delete('/api/evaluate', function(req, res) {
    let uid = req.body.uid
    let token = req.body.token
    let eid = req.body.eid
    evaluateDao.deleteEvaluate(uid, token, eid, function(ret) {
        res.json({
            code: ret.code,
            msg: ret.msg
        })
    })
})

module.exports = router; // 将路由暴露出去
